package m202405.d13;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/5/13 12:21
 */
public class Q94 {

    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        while (root != null || !stack.isEmpty()) {
            while (root != null) {
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            res.add(root.val);
            root = root.right;

        }
        return res;
    }

    public List<Integer> inorderTraversal1(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        it(root, res);
        return res;
    }

    private void it(TreeNode node, List<Integer> res) {
        if (node == null) {
            return;
        }
        it(node.left, res);
        res.add(node.val);
        it(node.right, res);
    }
}
